package mao.leetcode.offer;

/**
 * @author Totoro
 * @date 2020/8/12
 * @explain:
 */
public class interview54 {
    public static void main(String[] args) {
        Solution solution = new Solution();

    }

    static class  Solution {
        static int count = 0,k,res;  //还是使用共享变量简单。容易思考。
        public int kthLargest(TreeNode root, int k) {
            this.k = k;
            count = 0; //leetcode 注意初始化，每次测试都需要重新初始化。
            helper(root);
            return res;
        }

        public static void helper(TreeNode node){
            if (null == node || count>=k) return ;

            helper(node.right);

            count++;
            if(count == k) res = node.val;

            helper(node.left);
        }
    }

    public class TreeNode {
         int val;
        TreeNode left;
         TreeNode right;
         TreeNode(int x) { val = x; }
    }
}
